﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages.Html;
using FluentValidation;
using FluentValidation.Internal;
using FluentValidation.Mvc;
using FluentValidation.Validators;
using SDF.Core;
using SDFAuth.Models;

namespace Nop.Web.Validators.Boards
{
    public class LoginModelValidator : AbstractValidator<LoginModel>
    {

        public LoginModelValidator()
        {
            //To set the cascade mode globally, you can set the CascadeMode property on the static ValidatorOptions class during your application's startup routine:
            //ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;

            this.CascadeMode = CascadeMode.StopOnFirstFailure;

            RuleFor(u => u.UserName)
                                    .NotEmpty().WithMessage("用户名不能为空")
                                    .Length(2, 30).WithMessage("长度错误")
//                                    .LessThanOrEqualTo(_ => _.Password).WithMessage("应小于验证码")
                                    ;

            RuleFor(u => u.Password)
                                   .NotEmpty().WithMessage("密码不能为空");

            RuleFor(u => u.ValidateCode).Must(CheckValidateCode).WithMessage("验证码错误");
        }

        private bool CheckValidateCode(string ValidateCode)
        {
            var session = HttpContext.Current.Session;
            if (session[Consts.LoginValidateCode] == null || (session[Consts.LoginValidateCode] != null && ValidateCode != session[Consts.LoginValidateCode].ToString()))
            {
                return false;
            }
            return true;
        }
    }

/*    public class EqualToValueFluentValidationPropertyValidator : FluentValidationPropertyValidator
    {
        public EqualToValueFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
            : base(metadata, controllerContext, rule, validator)
        {
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            if (!this.ShouldGenerateClientSideRules())
            {
                yield break;
            }
            var validator = (EqualValidator)Validator;

            var errorMessage = new MessageFormatter()
                .AppendPropertyName(Rule.GetDisplayName())
                .AppendArgument("ValueToCompare", validator.ValueToCompare)
                .BuildMessage(validator.ErrorMessageSource.GetString());

            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = errorMessage;
            rule.ValidationType = "equaltovalue";
            rule.ValidationParameters["valuetocompare"] = validator.ValueToCompare;
            yield return rule;
        }
    }*/

    public class LessThanOrEqualToFluentValidationPropertyValidator : FluentValidationPropertyValidator
    {
        public LessThanOrEqualToFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
            : base(metadata, controllerContext, rule, validator)
        {
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            if (!this.ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var validator = Validator as LessThanOrEqualValidator;

            var errorMessage = new MessageFormatter()
                .AppendPropertyName(this.Rule.GetDisplayName())
                .BuildMessage(validator.ErrorMessageSource.GetString());

            var rule = new ModelClientValidationRule
            {
                ErrorMessage = errorMessage,
                ValidationType = "lessthanorequaldate"
            };
            rule.ValidationParameters["other"] = CompareAttribute.FormatPropertyForClientValidation(validator.MemberToCompare.Name);
            yield return rule;
        }
    }
}